package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.logistics.constant.Constants;
import com.ssy.lingxi.logistics.entity.LogisticsOrder;
import com.ssy.lingxi.logistics.service.ILogisticsOrderWorkFlowService;
import com.ssy.lingxi.workflow.api.feign.ProcessFeign;
import com.ssy.lingxi.workflow.api.model.vo.request.ExternalProcessQueryVO;
import com.ssy.lingxi.workflow.api.model.vo.request.InternalProcessQueryVO;
import com.ssy.lingxi.workflow.api.model.vo.request.TaskExecuteVO;
import com.ssy.lingxi.workflow.api.model.vo.request.TaskStartVO;
import com.ssy.lingxi.workflow.api.model.vo.response.SimpleProcessDefVO;
import com.ssy.lingxi.workflow.api.model.vo.response.SimpleTaskCompleteVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 物流单工作流服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/1/26
 */
@Slf4j
@Service
public class LogisticsOrderWorkFlowServiceImpl implements ILogisticsOrderWorkFlowService {

    @Resource
    private ProcessFeign processFeign;

    @Override
    public Wrapper<SimpleTaskCompleteVO> startTaskProcess(LogisticsOrder logisticsOrder, UserLoginCacheDTO loginUser) {
        TaskStartVO startVO = new TaskStartVO();
        startVO.setProcessKey(Constants.Workflow.key);
        startVO.setMemberId(loginUser.getMemberId());
        startVO.setRoleId(loginUser.getMemberRoleId());
        startVO.setDataId(logisticsOrder.getId());
        return processFeign.startSimpleProcess(startVO);
    }

    @Override
    public Wrapper<SimpleTaskCompleteVO> startThenCompleteTaskProcess(LogisticsOrder logisticsOrder, UserLoginCacheDTO loginUser) {
        TaskStartVO startVO = new TaskStartVO();
        startVO.setProcessKey(Constants.Workflow.key);
        startVO.setMemberId(loginUser.getMemberId());
        startVO.setRoleId(loginUser.getMemberRoleId());
        startVO.setDataId(logisticsOrder.getId());
        return processFeign.startSimpleProcessThenCompleteFirstTask(startVO);
    }

    @Override
    public Wrapper<SimpleTaskCompleteVO> completeTask(LogisticsOrder logisticsOrder, Integer agree) {
        TaskExecuteVO executeVO = new TaskExecuteVO();
        executeVO.setProcessKey(Constants.Workflow.key);
        executeVO.setMemberId(logisticsOrder.getMemberId());
        executeVO.setRoleId(logisticsOrder.getRoleId());
        executeVO.setDataId(logisticsOrder.getId());
        executeVO.setTaskId(logisticsOrder.getTaskId());
        executeVO.setAgree(agree);

        return processFeign.completeSimpleTask(executeVO);
    }

    @Override
    public Wrapper<SimpleProcessDefVO> listInternalProcessSteps(Long memberId, String processKey, String taskId) {
        InternalProcessQueryVO queryVO = new InternalProcessQueryVO();
        queryVO.setProcessKey(processKey);
        queryVO.setMemberId(memberId);
        queryVO.setTaskId(taskId);

       return processFeign.findSimpleInternalTaskDefinitions(queryVO);
    }

    @Override
    public Wrapper<SimpleProcessDefVO> listExternalProcessSteps(Long roleId, Long subRoleId, String processKey, String taskId) {
        ExternalProcessQueryVO queryVO = new ExternalProcessQueryVO();
        queryVO.setProcessKey(processKey);
        queryVO.setRoleId(roleId);
        queryVO.setSubRoleId(subRoleId);
        queryVO.setTaskId(taskId);

        return processFeign.findSimpleExternalTaskDefinitions(queryVO);
    }
}